package com.bd.rj.backstage.controller;



import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bd.rj.backstage.service.IEnterpriseService;
import com.bd.rj.backstage.service.IPostService;
import com.bd.rj.common.entity.Enterprise;
import com.bd.rj.common.entity.Post;
import com.bd.rj.common.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/post")
public class PostController {
    @Autowired
    IPostService postService;
    @Autowired
    IEnterpriseService enterpriseService;
    /**
     * 查询全部岗位信息
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/queryall",method = RequestMethod.GET)
    public PageInfo<Post> queryall(
            @RequestParam(value =  "pageNum",defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        String s = StpUtil.getRoleList().get(0);
        PageHelper.startPage(pageNum,pageSize);
        List<Post> list = new ArrayList<Post>();
        if (s.equals("2")) {
        QueryWrapper<Post> qw = new QueryWrapper<>();
            qw.eq("uid", StpUtil.getLoginIdAsLong());
           list = postService.list(qw);
        }else{
           list = postService.list();
        }
        PageInfo<Post> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 根据岗位名查询岗位信息
     * @param pageNum
     * @param postName
     * @return
     */
    @RequestMapping(value = "/getquery",method = RequestMethod.GET)
    public IPage<Post> query(
            @RequestParam(value = "pagenum",defaultValue = "1") int pageNum ,
            @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
            @RequestParam(value = "postname") String postName){
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("postname",postName);
        IPage<Post> postIPage = postService.getPostByPage(pageNum,pageSize,queryWrapper);
        return postIPage;
    }

    /**
     * 根据id获取岗位信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/queryid", method = RequestMethod.GET)
    public Map<String, Object> queryId(@RequestParam(value = "id") Long id) {
        return postService.queryId(id);
    }
    /**
     * 根据岗位类型查询岗位信息
     * @param pageNum
     * @param type
     * @return
     */
    @RequestMapping(value = "/typequery",method = RequestMethod.GET)
    public IPage<Post> typequery(@RequestParam(value = "pagenum" ,defaultValue = "1") int pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
                                 @RequestParam(value = "type") String type){
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("type",type);
        IPage<Post>postIPage = postService.getPostByPage(pageNum,pageSize,queryWrapper);
        return postIPage;
    }

    /**
     * 根据岗位状态查询岗位信息
     * @param pageNum
     * @param status
     * @return
     */
    @RequestMapping(value = "/statusquery",method = RequestMethod.GET)
    public IPage<Post> statusquery(
            @RequestParam(value = "pagenum",defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
            @RequestParam(value = "status") Integer status){
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",status);
        IPage<Post> postIPage = postService.getPostByPage(pageNum,pageSize,queryWrapper);
        return postIPage;
    }

    /**
     * 根据创建时间查询岗位信息
     * @param pageNum
     * @param createtime
     * @return
     */
    @RequestMapping(value = "/timequery",method = RequestMethod.GET)
    public IPage<Post> createtimequery(
            @RequestParam(value = "pagenum",defaultValue = "1") int pageNum ,
            @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
            @RequestParam(value = "createtime") String createtime){
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("createtime" , createtime);
        IPage<Post> postIPage = postService.getPostByPage(pageNum,pageSize,queryWrapper);
        return postIPage;
    }

    /**
     * 删除岗位
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public Map<String,Object> delete(Long id){
        Map<String,Object> map = new HashMap<>();
        String result = postService.deletePost(id);
        map.put("code",200);
        map.put("msg","删除成功");
        map.put("data",result);
        return map;
    }

    /**
     * 动态查询
     * @param pagenum
     * @param pageSize
     * @param company
     * @param postname
     * @param type
     * @param status
     * @return
     */
    @RequestMapping(value = "/query",method = RequestMethod.GET)
    public Map<String,Object> queryCondition(
            @RequestParam(value = "pageNum", defaultValue = "1") int pagenum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(value = "company", required = false) String company,
            @RequestParam(value = "postname", required = false) String postname,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "status", required = false) Integer status){
        Post post = new Post();
        post.setCompany(company);
        post.setPostname(postname);
        post.setType(type);
        post.setStatus(status);
        IPage<Map> postIPage = postService.getEnterpriseByConditionAndPage(pagenum,pageSize,post);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询");
        map.put("data", postIPage);
        return map;
    }
    /**
     * 添加岗位信息
     * @param post
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Map<String,Object> add(@RequestBody Post post){
        long loginIdAsLong = StpUtil.getLoginIdAsLong();
        System.out.println(post.getAddress()+"获得到的是这样的");
        QueryWrapper<Enterprise> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ename", post.getCompany());
        Enterprise enterprise = enterpriseService.getOne(queryWrapper);
        String ename = enterprise.getEname();
        post.setEid(enterprise.getId());
        post.setCompany(ename);
        Map<String,Object> map = new HashMap<>();
        post.setUid(loginIdAsLong);
        String result = postService.addPost(post);
        map.put("code",200);
        map.put("msg","添加岗位");
        map.put("data",result);
        return map;
    }

    /**
     * 修改岗位
     * @param post
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Map<String,Object> update(@RequestBody Post post){
        Map<String,Object> map = new HashMap<>();
        String result = postService.updatePost(post);
//        if (post.getStatus()!=null){
//            long loginIdAsLong = StpUtil.getLoginIdAsLong();
//            Map<String,Object> postRecord = new HashMap<>();
//            postRecord.put("uid",loginIdAsLong);
//            postRecord.put("pid", post.getId());
//            postRecord.put("status",post.getStatus());
//            postRecord.put("reasons",post.getReasons());
//            postService.insertPostRecord(postRecord);
//        }
        map.put("code",200);
        map.put("msg","修改岗位");
        map.put("data",result);
        return map;
    }

    /**
     * 根据老师id查询发布的岗位
     * @param pagenum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/queryUid",method = RequestMethod.GET)
    public IPage<Post> queryUid(
            @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
            @RequestParam(value = "pagesize", defaultValue = "10") int pageSize
            ){
        long loginIdAsLong = StpUtil.getLoginIdAsLong();
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",loginIdAsLong);
        IPage<Post> postIPage = postService.getPostByPage(pagenum,pageSize,queryWrapper);
        return postIPage;
    }
}
